#
# CONFIDENTIAL AND PROPRIETARY
#
# Unpublished Copyright (c) 2023-2024 Hsiang Hsu, All Rights Reserved.
#

TARGET := F103

# Global options
DEBUG ?= 0
OUT ?= build

# CMSIS
ASM_SOURCES += \
	CubeIDE/startup_stm32f103xb.s

C_INCLUDES += \
	-IDrivers/CMSIS/Include

C_SOURCES += \
	Drivers/CMSIS/system_stm32f1xx.c

# LL Library，按照需要的库进行添加
C_SOURCES += \
	Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_gpio.c \
	Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_utils.c \
	Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_exti.c \
	Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_dma.c \
	Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_usart.c \
	Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_crc.c \
	Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_pwr.c

C_INCLUDES += \
	-IDrivers/STM32F1xx_HAL_Driver/Inc

# TinyUSB
C_SOURCES += \
	Middlewares/TinyUSB/tusb.c \
	Middlewares/TinyUSB/common/tusb_fifo.c \
	Middlewares/TinyUSB/device/usbd.c \
	Middlewares/TinyUSB/device/usbd_control.c \
	Middlewares/TinyUSB/class/cdc/cdc_device.c \
	Middlewares/TinyUSB/portable/st/stm32_fsdev/dcd_stm32_fsdev.c

C_INCLUDES += \
	-IMiddlewares/TinyUSB

# Applications
C_SOURCES += \
	Applications/usb_descriptors.c

C_INCLUDES += \
	-IApplications

# System
C_SOURCES += \
	System/bsp_init.c \
	System/stm32f1xx_it.c 

C_INCLUDES += \
	-ISystem

C_SOURCES += \
	User/main.c 

C_INCLUDES += \
	-IUser

# Macros
C_DEFINES += \
	-DUSE_FULL_LL_DRIVER \
	-DSTM32F103xB \
	-DHSE_VALUE=8000000 \
	-DCFG_TUSB_MCU=OPT_MCU_STM32F1

# Linker script
LDSCRIPT := CubeIDE/STM32F103C8TX_FLASH.ld

########################################################
###  DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING  ###
########################################################

CROSS_COMPILE ?= arm-none-eabi-
CC := $(CROSS_COMPILE)gcc
AS := $(CROSS_COMPILE)gcc -x assembler-with-cpp
CP := $(CROSS_COMPILE)objcopy
SZ := $(CROSS_COMPILE)size
HEX := $(CP) -O ihex
BIN := $(CP) -O binary -S

STLINK ?= st-

CC_FLAGS += \
	-mcpu=cortex-m3 \
	-mfloat-abi=soft \
	-mthumb -mno-thumb-interwork \
	-Wall -Wno-unused-function \
	-fdata-sections -ffunction-sections

ASMFLAGS += $(CC_FLAGS)
CFLAGS += $(C_DEFINES) $(C_INCLUDES) $(CC_FLAGS) -std=c99

# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
else
CFLAGS += -O2
endif

LDFLAGS += \
	$(CC_FLAGS) \
	-specs=nano.specs \
	-T$(LDSCRIPT) \
	-lc -lm -lnosys \
	-Wl,-Map=$(OUT)/$(TARGET).map,--cref \
	-Wl,--gc-sections


all: $(OUT)/$(TARGET).elf $(OUT)/$(TARGET).hex $(OUT)/$(TARGET).bin

# list of C program objects
OBJECTS = $(addprefix $(OUT)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(OUT)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

$(OUT)/%.o: %.c Makefile | $(OUT)
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(OUT)/$(notdir $(<:.c=.lst)) $< -o $@

$(OUT)/%.o: %.s Makefile | $(OUT)
	$(AS) -c $(ASMFLAGS) $< -o $@

$(OUT)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(OUT)/%.hex: $(OUT)/%.elf | $(OUT)
	$(HEX) $< $@

$(OUT)/%.bin: $(OUT)/%.elf | $(OUT)
	$(BIN) $< $@

$(OUT):
	mkdir $@

clean:
	-rm -fR $(OUT)

-include $(wildcard $(OUT)/*.d)

flash: all
	$(STLINK)info --probe --connect-under-reset
	$(STLINK)flash erase && $(STLINK)flash write $(OUT)/$(TARGET).bin 0x08000000
